一、Docker install
https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/
Docker 提供了一个自动配置与安装的脚本,支持 Debian、RHEL、SUSE 系列及衍生系统的安装。
1 2 3 4 5 export DOWNLOAD_URL="https://mirrors.tuna.tsinghua.edu.cn/docker-ce" # 如您使用 curl curl -fsSL https://get.docker.com/ | sh # 如您使用 wget wget -O- https://get.docker.com/ | sh
如果你之前安装过 docker,请先删掉
1 sudo yum remove docker docker-common docker-selinux docker-engine
安装一些依赖
1 sudo yum install -y yum-utils device-mapper-persistent-data lvm2
根据你的发行版下载repo文件: CentOS/RHEL Fedora
1 wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
把软件仓库地址替换为 TUNA:
1 sudo sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
安装docker-ce:
1 2 3 4 sudo yum makecache fast sudo yum install docker-ce systemctl enable docker && systemctl restart docker
开启linux内核转发
1 2 3 4 5 6 7 8 cat >>/etc/sysctl.conf<<EOF net.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.all.rp_filter = 0 EOF sysctl -p
docker拉取镜像加速
1 2 3 4 5 6 7 8 9 10 11 12 13 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors" : [ "http://hub-mirror.c.163.com" , "https://docker.mirrors.ustc.edu.cn" ] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
免sudo
1 2 3 4 5 6 7 8 sudo groupadd docker sudo gpasswd -a ${USER} docker sudo service docker restart newgrp - docker
二、Docker常见镜像部署 部署registry镜像仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 sudo tee /etc/docker/daemon.json <<-'EOF' { "insecure-registries" : [ "youHostIp/domainName:5000" ], "registry-mirrors" : [ "http://hub-mirror.c.163.com" , "https://docker.mirrors.ustc.edu.cn" ] } EOF docker run -d -p 5000:5000 --restart=always --name registry docker.io/registry:latest systemctl daemon-reload && systemctl restart docker curl 192.168.45.129:5000/v2/_catalog curl 192.168.45.129:5000/v2/centos/tags/list
部署harbor镜像仓库
1 2 3 4 5 6 7 https://github.com/goharbor/harbor/releases tag_name=`curl -s https://api.github.com/repos/goharbor/harbor/releases/latest | grep tag_name | cut -f4 -d "\"" ` wget -Oc /opt/harbor-offline-installer-${tag_name} .tgz \ https://github.com/goharbor/harbor/releases/download/${tag_name} /harbor-offline-installer-${tag_name} .tgz
部署mysql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 docker run -itdp 3307:3306 --name mysql \ --restart=always \ -e MYSQL_ROOT_PASSWORD=db123456 \ -e TZ=Asia/Shanghai \ -v /data/mysql:/var/lib/mysql \ mysql:5.7 \ --character-set-server=utf8 \ --collation-server=utf8_unicode_ci \ --character-set-client-handshake=FALSE MYSQL_ROOT_PASSWORD MYSQL_DATABASE MYSQL_USER, MYSQL_PASSWORD MYSQL_ALLOW_EMPTY_PASSWORD MYSQL_RANDOM_ROOT_PASSWORD MYSQL_ONETIME_PASSWORD MYSQL_INITDB_SKIP_TZINFO
部署redis
1 2 3 4 5 6 7 8 docker run -itdp 6379:6379 \ --name redis \ --privileged=true \ --restart=always \ -v /data/redis/data:/data \ redis:latest \ --requirepass "redisPassword"
部署nginx
优雅的用docker命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}" docker images --format "table docker tag {{.ID}} {{.Repository}}:{{.Tag}}" docker images --format "table docker push {{.Repository}}:{{.Tag}}" docker run -tid --name centos \ --privileged \ docker.io/centos:7 /usr/sbin/init > 参考 https://docs.docker.com/engine/reference/run/ no 默认策略,在容器退出时不重启容器 on-failure 在容器非正常退出时(退出状态非0),才会重启容器 on-failure:3 在容器非正常退出时重启容器,最多重启3次 always 在容器退出时总是重启容器 unless-stopped 在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
镜像构建
Dockerfile
容器编排
docker-compose
kubenetes (k8s)
LAMP 这里我先介绍,第一种。
1 2 3 $ docker search -s 10 lamp $ docker pull docker.io/tutum/lamp $ docker images
创建LAMP容器
1 2 $ mkdir /mysql_data$ docker run -d --name=lamp -p 8080:80 -p 3306:3306 -v /mysql_data:/var/lib/mysql docker.io/tutum/lamp
将宿主机的目录“/mysql_data”映射到容器的“/var/lib/mysql”目录。这是因为默认情况下数据库的数据库文件和日志文件都会存放于容器的AUFS文件层,这不仅不使得容器变得越来越臃肿,不便于迁移、备份等管理,而且数据库的性能也会受到影响。因此建议挂载到宿主机的目录到容器内。 接下来进入容器
1 2 # docker exec -it lamp /bin/bash # mysql_secure_installation //初始化数据库
按下回城键你会看见结尾如下的对话。 Enter current password for root (enter for none):<–初次运行直接回车 Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车 New password: <– 设置root用户的密码 Re-enter new password: <– 再输入一次你设置的密码 Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车 Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车 Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车 Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车
All done! If you’ve completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB! 初始化MariaDB完成,接下来测试登录
好的现在你只要将你的模板导入即可,至于详细设置不同网站有所不同 安装typecho个人博客模板
1 2 3 4 5 6 7 8 9 apt update cd /var/www/html rm -rf * apt install -y wget wget http://typecho.org/downloads/1.1-17.10.30-release.tar.gz tar zxf -C type 1.1-17.10.30-release.tar.gz mv type/* ./ cd .. chmod 777 -R html
Docker基本命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 controller compute master slave1 server client vi /etc/vsftpd/vsftpd.conf 添加anon_root=/opt/ IPADDR=192.1681.200.xx NETMASK=255.255.255.0 GATEWAY=192.1681.200.1 DNS1=114.114.114.114 [docker] name=docker baseurl= gpgcheck=0 enable =1systemctl stop firewalld systemctl disable firewalld vi /etc/sysctl.conf net.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.all.rp_filter = 0 如果还没有 docker group 就添加一个: sudo groupadd docker 将用户加入该 group 内。然后退出并重新登录就生效啦。 sudo gpasswd -a ${USER} docker 重启 docker 服务 sudo service docker restart 切换当前会话到新 group 或者重启 X 会话 newgrp - docker sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors" : [ "http://hub-mirror.c.163.com" , "https://docker.mirrors.ustc.edu.cn" , "https://mirror.ccs.tencentyun.com" , "https://nv9ab05p.mirror.aliyuncs.com" ] } EOF sudo systemctl daemon-reload sudo systemctl restart docker ADD_REGISTRY='--add-registry 10.0.0.51:5000' INSECURE_REGISTRY='--insecure-registry 10.0.0.51:5000' ADD_REGISTRY='--add-registry server:5000' INSECURE_REGISTRY='--insecure-registry server:5000' docker run -d -p 5000:5000 --restart=always --name registry docker.io/registry:latest docker run -d -p 8080:8080 --restart=unless-stopped --name rancher rancher/server:v1.6.5 netstat -auntp |grep 80 yum install -y mariadb mariadb-server bridge-utils lsof systemctl enable mariadb systemctl restart mariadb mysql_secure_installation [root@server rancher1.6.5] 主机注册地址 主机连接Rancher API的Base URL是? http://192.168.200.20:8080 docker rmi $(docker images -qa) docker rm -f $(docker ps -qa) docker run -it -p 80:80 --name web server:5000/nginx:latest /bin/bash -i -t tty 终端 配合/bin/bash -d 后台启动 -p 端口物理机 --name 指定容器名 -P 随机映射端口 -p hostPort:containerPort -p ip:hostPort:containerPort -p ip::containerPort -p hostPort:containerPort:udp -p 8080:8080 -p 443:443 docker exec -it web /bin/bash [root@quanlook ~] Usage: docker update [OPTIONS] CONTAINER [CONTAINER...] Update configuration of one or more containers Aliases: docker container update, docker update Options: --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota --cpu-rt-period int Limit the CPU real-time period in microseconds --cpu-rt-runtime int Limit the CPU real-time runtime in microseconds -c, --cpu-shares int CPU shares (relative weight) --cpus decimal Number of CPUs --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) -m, --memory bytes Memory limit --memory-reservation bytes Memory soft limit --memory-swap bytes Swap limit equal to memory plus swap: -1 to enable unlimited swap --pids-limit int Tune container pids limit (set -1 for unlimited) --restart string Restart policy to apply when a container exits docker update \ --restart=always \ --publish--add 81:80 [CONTAINER ID/CONTAINER NAME] docker container update --publish-rm 8080:808 --publish-add 8080:80 my_container docker run -v /path/to/host/dir:/path/to/container/dir image docker stop container_name docker container update --detach=false --publish-rm /path/to/host/dir container_name docker container update --publish-add /new/host/dir:/path/to/container/dir container_name docker start container_name docker commit -a "[email protected] " -m "修改了/usr/share/nginx/html/" web nginx:v0.1 docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}" IMAGE ID REPOSITORY TAG 5f515359c7f8 redis latest 05a60462f8ba nginx latest fe9198c04d62 mongo 3.2 f753707788c5 ubuntu 16.04 f753707788c5 ubuntu latest 1e0c3dd64ccd ubuntu 14.04 [root@server ~] [root@server ~] 2、三个容器之间使用共享卷 docker run -itd --name web1 -p 81:80 -v /usr/share/nginx/html nginx docker run -itd --volumes-from web1 --name web2 -p 82:80 nginx docker run -itd --volumes-from web1 --name web3 -p 83:80 nginx docker exec -it web1 /bin/bash echo "I am web1." > /usr/share/nginx/html/index.html[root@server images] [root@server ~] [root@server ~] [root@server ~] e103676b5199ff766cb06b71fcb4c438fc083b4d4e044863db0944370c0fb914 [root@server ~] map[/opt/xiandian:{}] [root@registry ~] [root@registry ~] 9dad61c107d56b64319f4c20ffc1e7168a66c35ebb97ff51af1f119f4e621a38 [root@registry ~] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9dad61c107d5 nginx:latest "/bin/bash" 29 seconds ago Up 27 seconds 0.0.0.0:80->80/tcp, 443/tcp nginx 24bcccdd1598 172.30.15.50:5000/registry:latest "/entrypoint.sh /etc/" 20 hours ago Up 20 hours 0.0.0.0:5000->5000/tcp registry [root@registry ~] root@9dad61c107d5:/ root@9dad61c107d5:/ root@9dad61c107d5:/usr/share/nginx/html removed 'index.html' root@9dad61c107d5:/usr/share/nginx/html exit [root@registry ~] [root@registry opt] Welcome to XianDian! this is container! Thank you for using nginx. [root@registry opt] [root@registry docker_images] Welcome to XianDian! this is container! Thank you for using nginx! centos7 docker容器报 docker Failed to get D-Bus connection 错误 systemctl start nginx 报错内容:Failed to get D-Bus connection: Operation not permitted 报这个错的原因是dbus-daemon没能启动。systemctl并不是不能使用。启动时添加--privileged /usr/sbin/init 即可。docker容器会自动将dbus等服务启动起来。如下: docker run --privileged -ti --name centos docker.io/centos:7 /usr/sbin/init
docker私有云 1 2 3 4 5 6 7 8 9 10 11 12 mkdir ~/ownclouddocker run -id --name owncloud \ -p 8081:80 --restart=always \ -v ~/owncloud:/var/www/html/data owncloud:latest mkdir ~/nextclouddocker run -id --name nextcloud \ -p 8080:80 --restart=always \ -v ~/nextcloud:/var/www/html/data nextcloud:latest
Docker快速部署 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 HOST_NAME = server HOST_NAME_NODE = client HOST_IP = HOST_IP_NODE = systemctl stop firewalld.service systemctl disable firewalld.service sed -i 's/SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config setenforce 0 sed -i -e 's/#UseDNS yes/UseDNS no/g' -e 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config if [[ `ip a |grep -w $HOST_IP ` != '' ]];then hostnamectl set-hostname $HOST_NAME elif [[ `ip a |grep -w $HOST_IP_NODE ` != '' ]];then hostnamectl set-hostname $HOST_NAME_NODE else hostnamectl set-hostname $HOST_NAME fi sed -i -e "/$HOST_NAME /d" -e "/$HOST_NAME_NODE /d" /etc/hosts echo "$HOST_IP $HOST_NAME " >> /etc/hostsecho "$HOST_IP_NODE $HOST_NAME_NODE " >> /etc/hostssystemctl stop iptables iptables -F iptables -X iptables -Z echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.confecho 'net.ipv4.conf.default.rp_filter=0' >> /etc/sysctl.confecho 'net.ipv4.conf.all.rp_filter=0' >> /etc/sysctl.confsysctl -p setsebool -P allow_ftpd_full_access on setsebool -P ftp_home_dir on mkdir /etc/yum.repos.d/backupmv /etc/yum.repos.d/{*.repo,backup}tee /etc/yum.repos.d/local.repo<<EOF [centos] name=centos baseurl=ftp://server/centos gpgcheck=0 enabled=1 [iaas] name=iaas baseurl=ftp://server/iaas/iaas-repo gpgcheck=0 enabled=1 [pass] name=paas baseurl=ftp://server/paas/docker gpgcheck=0 enabled=1 EOF yum install -y docker-ce systemctl restart docker && systemctl enable docker echo "ADD_REGISTRY='--add-registry $HOST_IP :5000'" >>/etc/sysconfig/dockerecho "INSECURE_REGISTRY='--insecure-registry $HOST_IP :5000'" >>/etc/sysconfig/dockersystemctl daemon-reload systemctl restart docker docker info yum install -y mariadb mariadb-server bridge-utils systemctl enable mariadb && systemctl restart mariadb scp /etc/yum.repos.d/local.repo client:/etc/yum.repos.d/ftp.repo scp /etc/hosts client:/etc/hosts scp /etc/sysconfig/docker client:/etc/sysconfig/docker
docker-compose 安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 yum -y install jq curl apt install -y jq curl https://github.com/docker/compose/releases/download/v2.21.0/docker-compose-linux-x86_64 tag_name=`curl -s https://api.github.com/repos/docker/compose/releases/latest | grep tag_name | cut -f4 -d "\"" ` tag_name=`curl -s https://api.github.com/repos/docker/compose/releases/latest | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g' ` tag_name=`curl -s https://api.github.com/repos/docker/compose/releases/latest | jq .tag_name |sed 's/\"//g;s/,//g;s/ //g' ` sudo wget -cO /usr/local/bin/docker-compose \ https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/${tag_name} /docker-compose-`uname -s`-`uname -m` sudo curl -o /usr/local/bin/docker-compose \ https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/${tag_name} /docker-compose-`uname -s`-`uname -m` sudo curl -L \ https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/${tag_name} /docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose curl -sSLo /usr/local/bin/docker-composes \ https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/${tag_name} /docker-compose-`uname -s`-`uname -m` chmod +x /usr/local/bin/docker-composedocker-compose -v
内容整理中…….